﻿using MvcApplication.Common;
using MvcApplication.Entity;
using MvcApplication.Interface;
using System.Web.Mvc;
using System.Collections.Generic;

namespace MvcApplication.Controllers
{
    public class PostController : Controller
    {
        IPostService _postService;
        ICategoryService _categoryService;

        public PostController(IPostService postService,
            ICategoryService categoryService)
        {
            _postService = postService;
            _categoryService = categoryService;
        }

        public ActionResult Index(long id)
        {
            var post = _postService.GetPostByID(id);
            if (post == null) return HttpNotFound();

            return View(post);
        }

        [Permission(Entity.Menu.文章, Entity.PermissionType.访问)]
        public ActionResult List(Pager pager, PostFilter filter)
        {
            var postPage = _postService.GetPosts(pager, filter);
            return View(postPage);
        }

        [Permission(Entity.Menu.文章, Entity.PermissionType.编辑)]
        public ActionResult Edit(long? id)
        {
            Post post = new Post();
            post.Status = PostStatus.发布;
            if (id.HasValue)
            {
                post = _postService.GetPostByID(id.Value);
                post.Categories.ForEach(t => t.Posts = null);
            }
            ViewBag.Categories = _categoryService.GetCategories();
            return View(post);
        }

        [Permission(Entity.Menu.文章, Entity.PermissionType.编辑)]
        [HttpPost, ValidateInput(false)]
        public JsonResult Save(Post post, List<long> categoryIDs, bool IsSlider = false)
        {
            if (categoryIDs == null) categoryIDs = new List<long>();
            var retsult = _postService.Save(post, categoryIDs);

            retsult.Categories.ForEach(t => t.Posts = null);
            return Json(retsult);
        }

        [Permission(Entity.Menu.文章, Entity.PermissionType.访问)]
        public ActionResult FastEdit(Post post)
        {
            if (post.ID > 0)
                post = _postService.GetPostByID(post.ID);

            return View(post);
        }

        [Permission(Entity.Menu.文章, Entity.PermissionType.编辑)]
        [HttpPost, ValidateInput(false)]
        public JsonResult SaveFastEdit(Post post)
        {
            var data = _postService.SaveFastEdit(post);
            return Json(data);
        }

        /// <summary>
        /// 批量添加文章到回收站
        /// </summary>
        /// <param name="postIds"></param>
        /// <returns></returns>
        [Permission(Entity.Menu.文章, Entity.PermissionType.删除)]
        public JsonResult ToRecycle(List<long> postIds)
        {
            _postService.ToRecycle(postIds);
            return Json(true);
        }

        /// <summary>
        /// 批量物理删除文章
        /// </summary>
        /// <param name="postIds"></param>
        /// <returns></returns>
        [Permission(Entity.Menu.文章, Entity.PermissionType.删除)]
        public JsonResult Remove(List<long> postIds)
        {
            _postService.ToRecycle(postIds);
            return Json(true);
        }
    }
}
